#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;
const int N = 1e3 + 10;
int n,s;
int a[N],b[N];
int dp[N][N*6];

int main()
{
    cin >> n;
    for(int i = 1; i<=n; i++){
        cin >> a[i] >> b[i];
        s+= a[i] + b[i];
    }

    for (int i = 1; i <= n; i ++)
        for (int j = 0; j <= 6*n; j ++) dp[i][j] = INF;
   

    //首先初始化b，如果a[1] == b[1]，也不会有问题
    dp[1][b[1]] = 1,dp[1][a[1]] = 0;

    for (int i = 2; i <= n; i ++)   
        for (int j = 0; j <= 6*n; j ++){
            if (j-a[i] >= 0) dp[i][j] = min(dp[i][j], dp[i-1][j-a[i]]);  // 不交换
            if (j-b[i] >= 0) dp[i][j] = min(dp[i][j], dp[i-1][j-b[i]]+1);   // 交换
        }

    int minX = INF,minT = INF;  // 最小差值，最小交换次数

    for (int i = 0; i <= s; i ++)
    if (dp[n][i] != INF){
        if (abs(i-(s-i)) < minX){
            minX = abs(i-(s-i)); 
            minT = dp[n][i];
        }
        else if (abs(i-(s-i)) == minX) minT = min(minT, dp[n][i]);
    }

    cout << minT;
   
   return 0;
}
